package com.wswy.wzcx.ui.module.oilprice

import android.graphics.PointF
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.view.View
import android.widget.TableLayout
import com.blankj.utilcode.util.SizeUtils
import com.wswy.wzcx.R
import com.wswy.wzcx.module.base.CBaseFragment
import kotlinx.android.synthetic.main.f_oil_trend_fragment.*
import java.math.BigDecimal


class OilTrendFragment : CBaseFragment() {

    private companion object {
        const val TAG = "OilTrendFragment"
        private const val TYPE_95 = "95#"
        private const val TYPE_92 = "92#"
        private const val TYPE_D0 = "d0#"


        @JvmStatic
        fun newInstance(list: Bundle):CBaseFragment{

            return OilTrendFragment().apply {
                arguments =list
            }
        }

    }

    override fun getFragmentTitle(): String {
        return "调价趋势"
    }

    override fun getContentLayoutId(): Int {
        return R.layout.f_oil_trend_fragment
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

//        val list = arguments?.getParcelableArrayList<OilPriceVo>(Constants.EXTRA_DATA)?:kotlin.run {
//            finish()
//            return
//        }

        val list92 = arguments?.getParcelableArrayList<OilPriceVo>(TYPE_92)
        val list95 = arguments?.getParcelableArrayList<OilPriceVo>(TYPE_95)
        val listd0 = arguments?.getParcelableArrayList<OilPriceVo>(TYPE_D0)

        if (list92.isNullOrEmpty() && list95.isNullOrEmpty() && listd0.isNullOrEmpty()){
            finish()
            return
        }

        val type = arrayListOf<Pair<String,String>>()

        if (!list92.isNullOrEmpty()){
            type.add(Pair(TYPE_92,"92#汽油"))
        }
        if (!list95.isNullOrEmpty()){
            type.add(Pair(TYPE_95,"95#汽油"))
        }
        if (!listd0.isNullOrEmpty()){
            type.add(Pair(TYPE_D0,"0#柴油"))
        }


        val showWithTag:((String?)->Unit) = {
            when(it){
                TYPE_92 -> {
                    list92?.let {
                        showPriceTrend(it)
                    }
                }
                TYPE_95 -> {
                    list95?.let {
                        showPriceTrend(it)
                    }
                }
                TYPE_D0 -> {
                    listd0?.let {
                        showPriceTrend(it)
                    }
                }
            }
        }



        type.forEach {
            tabLayout.addTab(tabLayout.newTab().setText(it.second).setTag(it.first))
        }

        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {

                showWithTag.invoke(tab?.tag as? String)

            }

        })


        showWithTag.invoke(type.first().first)

    }





    private fun showPriceTrend(list: List<OilPriceVo>){

        tv_point_value.visibility = View.INVISIBLE

        val data = list.map { PointF(it.index.toFloat(),it.value) }

        val xTexts = list.map { it.xText }

        val min = list.minBy { it.value }
        val max =list.maxBy { it.value }


        if (min != null && max != null){

            //计算方式  (max-min)/4 四舍五入 得到y最小刻度

            val bb =BigDecimal(max.value.toString()).minus(BigDecimal(min.value.toString())).div(BigDecimal.valueOf(4))

            val yunit = bb.setScale(1, BigDecimal.ROUND_HALF_UP)

            val b2 = BigDecimal(max.value.toString()).add(yunit) //y最大值

            val yTexts = ArrayList<String>()
            for (i in 7 downTo 0){
                //根据刻度依次递减
                val offset = yunit.multiply(BigDecimal.valueOf(i.toLong()))
                yTexts.add(b2.minus(offset).toString())
            }

            brokenLineView.setYUnitValue(yunit.toFloat())
            brokenLineView.setYBaseValue(BigDecimal(yTexts.first()).minus(yunit).toFloat()) //y基础值=最小值刻度-刻度
            brokenLineView.setYTextUnits(yTexts)
            //Log.e(TAG, " --> ${yTexts}");
        }


        brokenLineView.setXUnitValue(1f)
                .setXTextUnits(xTexts)
                .setDateList(data)
                .startDraw()


        brokenLineView.selectionCallback = {idx,p1,p2,p3 ->

            data.getOrNull(idx)?.let {
                tv_point_value.visibility = View.VISIBLE
                tv_point_value.text = it.y.toString()
            }


            val showLeft = (idx == 0 || p1?.y?:0f < p3?.y?:0f)


            if (showLeft){
                //最左边

                tv_point_value.setBackgroundResource(R.drawable.tag_left)

                p2?.let {

                    tv_point_value.x = it.x + SizeUtils.dp2px(16f)
                    tv_point_value.y = it.y - SizeUtils.dp2px(12f)
                }
            }else {
                //最右边
                tv_point_value.setBackgroundResource(R.drawable.tag_right)

                p2?.let {

                    tv_point_value.x = it.x + SizeUtils.dp2px(16f) - tv_point_value.width
                    tv_point_value.y = it.y - SizeUtils.dp2px(12f)
                }

            }

        }
    }

}